//
//  TabBarItemView.m
//  TabBarView
//
//  Created by HaiSheng on 15/8/8.
//  Copyright (c) 2015年 huanghaisheng. All rights reserved.
//

#import "TabBarItemView.h"

NSString *kTitle = @"tab_title";
NSString *kImgNormal = @"tab_normal_img";
NSString *kImgHighlighted = @"tab_highlighted_img";
NSString *kNormalColor = @"tab_normal_color";
NSString *kHighlightedColor = @"tab_highlighted_color";

@implementation TabBarItemView

- (void)dealloc
{
    self.delegate = nil;
}

- (id)initWithFrame:(CGRect)frame ItemInfo:(NSDictionary *)itemInfo Style:(TabItemStyle)style {
    
    
    self = [super initWithFrame:frame];
    
    if (self) {
        
        self.itemStyle = style;
        self.title = itemInfo[kTitle];
        self.normalImage = [self getIconImage:itemInfo[kImgNormal]];
        self.highlightedImage = [self getIconImage:itemInfo[kImgHighlighted]];
        self.normalColor = [self getItemTextColor:itemInfo[kNormalColor]];
        self.highlightedColor = [self getItemTextColor:itemInfo[kHighlightedColor]];
        float width = frame.size.width;
        
        if (style == TabItemStyleTitle) {
            
            self.button = [UIButton buttonWithType:UIButtonTypeCustom];
            self.button.frame = CGRectMake(0.0, 0.0, frame.size.width, frame.size.height);
            [self.button addTarget:self action:@selector(clickedTabBar:) forControlEvents:UIControlEventTouchDown];
            [self addSubview:self.button];
            
            self.titleLabel = [[UILabel alloc] init];
            self.titleLabel.frame = CGRectMake(0.0, 0.0, frame.size.width, frame.size.height);
            self.titleLabel.font = [UIFont systemFontOfSize:17.0f];
            self.titleLabel.textAlignment = NSTextAlignmentCenter;
            self.titleLabel.textColor = self.normalColor;
            self.titleLabel.text = self.title;
            [self addSubview:self.titleLabel];
            
        } else if (style == TabItemStyleTitleAndIcon) {
            
            self.button = [UIButton buttonWithType:UIButtonTypeCustom];
            self.button.frame = CGRectMake(0.0, 0.0, frame.size.width, frame.size.height);
            [self.button addTarget:self action:@selector(clickedTabBar:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:self.button];
            
            
            self.iconView = [[UIImageView alloc] init];
            self.iconView.frame = CGRectMake(width/2.0 - 15.0 , 3.0, 30.0, 30.0);
            self.iconView.image = self.normalImage;
            [self addSubview:self.iconView];
            
            self.titleLabel = [[UILabel alloc] init];
            self.titleLabel.frame = CGRectMake(0.0, frame.size.height - 16.0, frame.size.width, 13.0);
            self.titleLabel.font = [UIFont systemFontOfSize:10.0f];
            self.titleLabel.textAlignment = NSTextAlignmentCenter;
            self.titleLabel.text = self.title;
            self.titleLabel.textColor = self.normalColor;
            self.titleLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
            [self addSubview:self.titleLabel];
        }
    }
    
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    CGRect frame = self.frame;
    if (self.itemStyle == TabItemStyleTitleAndIcon) {
        self.button.frame = CGRectMake(0.0, 0.0, frame.size.width, frame.size.height);
        self.iconView.frame = CGRectMake(frame.size.width/2.0 - 15.0 , 3.0, 30.0, 30.0);
        self.titleLabel.frame = CGRectMake(0.0, frame.size.height - 16.0, frame.size.width, 13.0);
    } else if (self.itemStyle == TabItemStyleTitle) {
        self.button.frame = CGRectMake(0.0, 0.0, frame.size.width, frame.size.height);
        self.titleLabel.frame = CGRectMake(0.0, 0.0, frame.size.width, frame.size.height);
    }
}


- (void)setNormalStatus
{
    self.titleLabel.textColor = self.normalColor;
    if (self.itemStyle == TabItemStyleTitleAndIcon && self.normalImage) {
        self.iconView.image = self.normalImage;
    }
}

- (void)setHighlightedStatus
{
    self.titleLabel.textColor = self.highlightedColor;
    if (self.itemStyle == TabItemStyleTitleAndIcon && self.highlightedImage) {
        self.iconView.image = self.highlightedImage;
    }
}

- (void)clickMe
{
    [self clickedTabBar:nil];
}

- (void)clickedTabBar:(id)sender
{
    if ([self.delegate respondsToSelector:@selector(shouldClickedTabBarItemView:)]) {
        if ([self.delegate shouldClickedTabBarItemView:self]) {
            [self setHighlightedStatus];
            [self.delegate didClickedTabBarItemView:self];
        }
    } else {
        [self setHighlightedStatus];
        [self.delegate didClickedTabBarItemView:self];
    }
}

- (void)setNormalColor:(UIColor *)normalColor {
    
    _normalColor = normalColor;
    [self setNormalStatus];
}

- (void)setHighlightedColor:(UIColor *)highlightedColor {
    
    _highlightedColor = highlightedColor;
    [self setHighlightedStatus];
}

- (void)setNormalImage:(UIImage *)normalImage {
    
    _normalImage = normalImage;
    [self setNormalStatus];
}

- (void)setHighlightedImage:(UIImage *)highlightedImage {
    
    _highlightedImage = highlightedImage;
    [self setHighlightedStatus];
}

- (UIImage *)getIconImage:(id)img {
    
    if ([img isKindOfClass:[UIImage class]]) {
        return img;
    } else if ([img isKindOfClass:[NSString class]]){
        return [UIImage imageNamed:img];
    } else {
        return nil;
    }
}

- (UIColor *)getItemTextColor:(id)color {
    
    if ([color isKindOfClass:[UIColor class]]) {
        return color;
    } else if ([color isKindOfClass:[NSString class]]){
        return [self colorWithHexString:color];
    } else {
        return nil;
    }
}

- (UIColor *)colorWithHexString:(NSString *)stringToConvert
{
    NSString *cString = [[stringToConvert stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    // String should be 6 or 8 characters
    
    if ([cString length] < 6) return [UIColor whiteColor];
    // strip 0X if it appears
    if ([cString hasPrefix:@"0X"]) cString = [cString substringFromIndex:2];
    if ([cString hasPrefix:@"#"]) cString = [cString substringFromIndex:1];
    if ([cString length] != 6) return [UIColor whiteColor];
    
    // Separate into r, g, b substrings
    
    NSRange range;
    range.location = 0;
    range.length = 2;
    NSString *rString = [cString substringWithRange:range];
    range.location = 2;
    NSString *gString = [cString substringWithRange:range];
    range.location = 4;
    NSString *bString = [cString substringWithRange:range];
    // Scan values
    unsigned int r, g, b;
    
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];
    
    return [UIColor colorWithRed:((float) r / 255.0f)
                           green:((float) g / 255.0f)
                            blue:((float) b / 255.0f)
                           alpha:1.0f];
}



@end
